import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../models/product_model.dart';
import '../../../models/thing_model.dart';
import '../../../providers/product_provider.dart';
import '../../../services/theme_service.dart';
import '../../../utils/json_utils.dart';

class ProductModelDetailController extends GetxController {
  final ProductProvider productProvider = ProductProvider();
  final ThemeService _themeService = Get.find<ThemeService>();
  
  // 获取传递的产品ID
  final int productId = Get.arguments['productId'] ?? 0;
  
  // 可观察变量
  final RxBool isLoading = true.obs;
  final RxBool hasError = false.obs;
  final RxString errorMessage = ''.obs;
  final Rx<Product?> product = Rx<Product?>(null);
  final RxList<ThingModel> thingModels = <ThingModel>[].obs;
  
  @override
  void onInit() {
    super.onInit();
    loadProductDetail();
  }
  
  // 加载产品详情
  Future<void> loadProductDetail() async {
    try {
      isLoading.value = true;
      hasError.value = false;
      
      final response = await productProvider.getProductDetail(productId);
      
      if (response.isSuccess && response.data != null) {
        product.value = response.data;
        // 加载该产品的物模型列表
        await loadThingModels();
      } else {
        hasError.value = true;
        errorMessage.value = response.msg;
      }
    } catch (e) {
      hasError.value = true;
      errorMessage.value = '获取产品详情失败: $e';
    } finally {
      isLoading.value = false;
    }
  }
  
  // 加载物模型列表
  Future<void> loadThingModels() async {
    try {
      final response = await productProvider.getThingModelList(
        productId: productId,
        pageSize: 50 // 加载较多记录
      );
      
      if (response.code == 200) {
        // 放入前清空列表 
        thingModels.clear();
        thingModels.assignAll(response.rows);
        
        debugPrint('物模型加载成功，共 ${response.rows.length} 个物模型');
      } else {
        debugPrint('API响应异常: code=${response.code}, msg=${response.msg}');
      }
    } catch (e) {
      // 物模型加载失败不影响产品详情显示
      debugPrint('获取物模型列表失败: $e');
    }
  }
  
  // 刷新数据
  Future<void> refreshData() async {
    await loadProductDetail();
  }
  
  // 获取类型图标颜色
  Color getTypeIconColor(int? nodeType) {
    switch (nodeType) {
      case 1: // 直连设备
        return _themeService.getThemedColor(
          lightColor: Colors.blue,
          darkColor: Colors.blue.shade300,
        );
      case 2: // 网关设备
        return _themeService.getThemedColor(
          lightColor: Colors.green,
          darkColor: Colors.green.shade300,
        );
      case 3: // 子设备
        return _themeService.getThemedColor(
          lightColor: Colors.orange,
          darkColor: Colors.orange.shade300,
        );
      default:
        return _themeService.getThemedColor(
          lightColor: Colors.grey,
          darkColor: Colors.grey.shade400,
        );
    }
  }
  
  // 获取状态文本
  String getStatusText(int? status) {
    switch (status) {
      case 0:
        return '开发中';
      case 1:
        return '已发布';
      default:
        return '未知';
    }
  }
  
  // 获取状态颜色
  Color getStatusColor(int? status) {
    switch (status) {
      case 0:
        return _themeService.getThemedColor(
          lightColor: Colors.orange,
          darkColor: Colors.orange.shade300,
        );
      case 1:
        return _themeService.getThemedColor(
          lightColor: Colors.green,
          darkColor: Colors.green.shade300,
        );
      default:
        return _themeService.getThemedColor(
          lightColor: Colors.grey,
          darkColor: Colors.grey.shade400,
        );
    }
  }
  
  // 获取节点类型文本
  String getNodeTypeText(int? nodeType) {
    switch (nodeType) {
      case 1:
        return '直连设备';
      case 2:
        return '网关设备';
      case 3:
        return '子设备';
      default:
        return '未知类型';
    }
  }
  
  // 获取数据格式文本
  String getDataFormatText(int? dataFormat) {
    switch (dataFormat) {
      case 1:
        return 'JSON';
      case 2:
        return '二进制';
      case 3:
        return '自定义';
      default:
        return '未知';
    }
  }
  
  // 获取认证方式文本
  String getAuthTypeText(int? authType) {
    switch (authType) {
      case 1:
        return '设备密钥';
      case 2:
        return '证书认证';
      case 3:
        return '设备ID认证';
      default:
        return '未知';
    }
  }
  
  // 获取物模型类型文本
  String getModelTypeText(int? modelType) {
    switch (modelType) {
      case 1:
        return '属性';
      case 2:
        return '服务';
      case 3:
        return '事件';
      default:
        return '未知';
    }
  }
} 